﻿////////////////////////////////////////////////
/// Провайдер форматирования fp-кода
/// Обеспечтвает форматирование fp-кода и загрузку интерпретатора
/// Работает с тэгом <fpcode>
/// Василий Кудрявцев
/// 1 декабря 2008 года

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using ScrewTurn.Wiki.PluginFramework;

namespace ScrewTurn.Wiki.PluginPack
{
    public class FpCodeInterpreterFormatter : IFormatterProvider
    {
        private Regex fpcode = new Regex(@"\<fpcode\>(.|\n|\r)*?\<\/fpcode\>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

        #region IFormatterProvider Members

        bool IFormatterProvider.PerformPhase1
        {
            get { return true; }
        }

        bool IFormatterProvider.PerformPhase2
        {
            get { return false; }
        }

        bool IFormatterProvider.PerformPhase3
        {
            get { return true; }
        }

        string IFormatterProvider.Format(string raw, ContextInformation context, FormattingPhase phase)
        {
            //Оборачиваем fpcode тэгом niwiki
            if (phase == FormattingPhase.Phase1 && context.Page != null)
            {
                var match = fpcode.Match(raw);
                while (match.Success)
                {

                    raw = raw.Insert(match.Index, "<nowiki>");
                    raw = raw.Insert(match.Index+match.Length+8, "</nowiki>");

                    match = fpcode.Match(raw, match.Index+16);

                }

                return raw;

            }
            if (phase == FormattingPhase.Phase3 && context.Page != null)
           {
               int counter = 0;
               var match = fpcode.Match(raw);
               while (match.Success)
               {
                   raw = raw.Remove(match.Index, match.Length);
                   var code = match.Value.Substring(8, match.Length - 17);
                   raw = raw.Insert(match.Index, 
                       FPAdditionalProviders.interpreter.SilverlightHTML.
                       Replace("DIVID", "fpsldiv"+counter).
                       Replace("SLID", "fpsl" + counter)).
                       Replace("{FPCODE}", code);
                   match = fpcode.Match(raw);
                   counter++;
               }

               return raw;
           }
           else
           {
               return raw;
           }
        }

        #endregion

        #region IProvider Members

        void IProvider.Init(IHost host, string config)
        {
            
        }

        void IProvider.Shutdown()
        {
           
        }

        ComponentInformation IProvider.Information
        {
            get { return new ComponentInformation("Silverlight FP interpreter", "Vasiliy Kudriavtsev", "mailto:vklava@yandex.ru"); }
        }

        #endregion
    }
}
